package jsp;

import util.CookieUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @author 石小俊
 * @date 2023年10月24日 9:42
 */
@WebServlet("*.cookie")
public class CookieServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        switch (path) {
            case "/add.cookie":
                addCookie(request, response);
                break;
            case "/find.cookie":
                findCookie(request, response);
                break;
            case "/set.cookie":
                setAge(request, response);
                break;
            case "/delete.cookie":
                deleteCookie(request, response);
                break;
            default:
                PrintWriter out = response.getWriter();
                out.println("<h1 style='color:red;'>您输入的请求不存在,请检查请求是否正确!</h1>");
                break;
        }
    }

    protected void addCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 创建一个Cookie对象
        // public Cookie(String name, String value)
        // 只能保存字符串
//        Cookie cookie = new Cookie("username", "admin");
//        // 在响应的同时将cookie带入到响应对象返回客户端
//        response.addCookie(cookie);
//
//        // 如果需要保存中文,必须对字符集进行处理
//        // 在Cookie中,提供了对应的工具类用于处理字符集
//        String name = "管理员";
//        // 保存中文数据前需要做的处理
//        name = URLEncoder.encode(name, "UTF-8");
//        Cookie cookie2 = new Cookie("name", name);
//        response.addCookie(cookie2);
        CookieUtil.addCookie("name","管理员",response);
        CookieUtil.addCookie("username","admin",response);
    }

    protected void findCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求中所有的Cookie数据
//        Cookie[] cookies = request.getCookies();
//        for (Cookie cookie : cookies) {
//            // 获取对应cookie时可能存在中文数据
//            // 在cookie中的中文数据都是经过处理的
//            // 因此,在取出中文数据的时候需要再次处理
//            // 将其字符集还原
//            String value = cookie.getValue();
//            value = URLDecoder.decode(value, "UTF-8");
//            System.out.println(cookie.getName() + ":" + value);
//        }
        String name = CookieUtil.findByName("name",request);
        String username = CookieUtil.findByName("username",request);
        System.out.println("name:"+name);
        System.out.println("username:"+username);
    }

    protected void setAge(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("password","123456");
        // 设置cookie生存时间
        // 默认情况下,当会话结束,cookie过期
        // 即:当浏览器关闭之后重新打开,之前保存的cookie全部清空
        // cookie.setMaxAge(秒):设置cookie生存多少秒
        // 设置之后其生存时间与会话是否结束无关
        cookie.setMaxAge(30);
        response.addCookie(cookie);
    }

    protected void deleteCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 对于删除,其本质上就是将对应的Cookie数据的生存时间设置为0
//        Cookie cookie = new Cookie("username", "");
//        cookie.setMaxAge(0);
//        response.addCookie(cookie);
        CookieUtil.deleteCookie("username",response);
    }
}
